package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.speeddating.entity.TagType;
import com.example.speeddating.entity.Tags;
import com.example.speeddating.entity.UserTag;
import com.example.speeddating.mapper.TagTypeMapper;
import com.example.speeddating.mapper.TagsMapper;
import com.example.speeddating.mapper.UserTagMapper;
import com.example.speeddating.service.TagTypeService;
import com.example.speeddating.service.TagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Service
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Autowired
    private TagTypeService tagTypeService;

    @Autowired
    private UserTagMapper userTagMapper;

    @Override
    public List<Tags> allTags(int type) {
        QueryWrapper<Tags> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("type_id", type);
        return tagsMapper.selectList(QueryWrapper);
    }

    @Override
    public List<Tags> allTags(int type, Long userId) {
        List<Tags> tags = allTags(type);
        for (Tags tag : tags) {
            QueryWrapper<UserTag> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tag_id", tag.getId());
            queryWrapper.eq("user_id", userId);
            UserTag userTag = userTagMapper.selectOne(queryWrapper);
            if (userTag != null) {
                tag.setSelect(true);
            }else {
                tag.setSelect(false);
            }
        }
        return tags;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTags(int tagId) {
        return tagsMapper.deleteById(tagId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTags(Tags tags) {
        return tagsMapper.updateTags(tags);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTags(Tags tags) {
        return tagsMapper.insert(tags);
    }

    @Override
    public List all() {
        ArrayList<Object> list = new ArrayList<>();
        List<TagType> tagTypes = tagTypeService.allTagType();
        for (TagType tagType : tagTypes) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",tagType.getId());
            map.put("name",tagType.getName());
            map.put("type",tagType.getType());
            List<Tags> tags = allTags(tagType.getId());
            map.put("data",tags);
            list.add(map);
        }
        return list;
    }

    @Override
    public List all_user(Long userId) {
        ArrayList<Object> list = new ArrayList<>();
        List<TagType> tagTypes = tagTypeService.allTagType();
        for (TagType tagType : tagTypes) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",tagType.getId());
            map.put("name",tagType.getName());
            map.put("type",tagType.getType());
            List<Tags> tags = allTags(tagType.getId(),userId);
            map.put("data",tags);
            list.add(map);
        }
        return list;
    }

    @Override
    public List<Tags> selectUserTagsByUserId(Long id) {
        return tagsMapper.selectUserTagsByUserId(id);
    }



}
